package com.xiaozhen.alarm.handler;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xiaozhen.alarm.domain.AlarmConfig;
import com.xiaozhen.alarm.domain.AlarmStrategy;
import com.xiaozhen.alarm.service.AlarmConfigService;
import com.xiaozhen.alarm.service.AlarmStrategyService;
import com.xiaozhen.common.core.domain.system.SysUser;
import com.xiaozhen.common.core.utils.StringUtils;
import com.xiaozhen.mail.service.SysMailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * HISTORY
 *
 * @author zhenweisheng
 * @date 2021/10/28 10:15
 * @description:
 */
@Service
public class DefaultAlarmHandler implements AlarmHandler {

    @Autowired
    private AlarmConfigService alarmConfigService;
    @Autowired
    private AlarmStrategyService alarmStrategyService;

    @Override
    public void handleJsonData(String tableName, Map<String,Object> jsonData) {
        //查询是否有报警配置
        List<AlarmConfig> alarmConfigs = alarmConfigService.selectAlarmConfigByTableName(tableName);
        if (alarmConfigs != null && alarmConfigs.size() > 0) {
            alarmConfigs.forEach(alarmConfig -> {
                String field = alarmConfig.getField();
                //获取字段值
                double fieldValue = (Double)jsonData.get(field);
                Double notifyThreshold = alarmConfig.getNotifyThreshold();
                Double warningThreshold = alarmConfig.getWarningThreshold();
                Double emergencyThreshold = alarmConfig.getEmergencyThreshold();
                //获取告警策略
                List<AlarmStrategy> alarmStrategyList = alarmStrategyService.selectAlarmConfigStrategy(alarmConfig.getConfigId());
                //获取运算符
                switch (alarmConfig.getSymbols()) {
                    case ">":
                        if (fieldValue > notifyThreshold) {
                            this.handleAlarmStrategy(jsonData, alarmStrategyList.stream().filter(alarmStrategy -> "1".equals(alarmStrategy.getAlarmLevel())).collect(Collectors.toList()));
                        }
                        if (fieldValue > warningThreshold) {
                            this.handleAlarmStrategy(jsonData, alarmStrategyList.stream().filter(alarmStrategy -> "2".equals(alarmStrategy.getAlarmLevel())).collect(Collectors.toList()));
                        }
                        if (fieldValue > emergencyThreshold) {
                            this.handleAlarmStrategy(jsonData, alarmStrategyList.stream().filter(alarmStrategy -> "3".equals(alarmStrategy.getAlarmLevel())).collect(Collectors.toList()));
                        }
                        break;
                    case "<":
                        if (fieldValue < notifyThreshold) {
                            this.handleAlarmStrategy(jsonData, alarmStrategyList.stream().filter(alarmStrategy -> "1".equals(alarmStrategy.getAlarmLevel())).collect(Collectors.toList()));
                        }
                        if (fieldValue < warningThreshold) {
                            this.handleAlarmStrategy(jsonData, alarmStrategyList.stream().filter(alarmStrategy -> "2".equals(alarmStrategy.getAlarmLevel())).collect(Collectors.toList()));
                        }
                        if (fieldValue < emergencyThreshold) {
                            this.handleAlarmStrategy(jsonData, alarmStrategyList.stream().filter(alarmStrategy -> "3".equals(alarmStrategy.getAlarmLevel())).collect(Collectors.toList()));
                        }
                        break;
                    case "=":
                        if (fieldValue == notifyThreshold) {
                            this.handleAlarmStrategy(jsonData, alarmStrategyList.stream().filter(alarmStrategy -> "1".equals(alarmStrategy.getAlarmLevel())).collect(Collectors.toList()));
                        }
                        if (fieldValue == warningThreshold) {
                            this.handleAlarmStrategy(jsonData, alarmStrategyList.stream().filter(alarmStrategy -> "2".equals(alarmStrategy.getAlarmLevel())).collect(Collectors.toList()));
                        }
                        if (fieldValue == emergencyThreshold) {
                            this.handleAlarmStrategy(jsonData, alarmStrategyList.stream().filter(alarmStrategy -> "3".equals(alarmStrategy.getAlarmLevel())).collect(Collectors.toList()));
                        }
                        break;
                    default:
                }
            });
        }
    }

    @Autowired
    private SysMailService sysMailService;

    /**
     * 处理对应告警策略的消息
     *
     * @param jsonData
     * @param alarmStrategyList
     */
    @Override
    public void handleAlarmStrategy(Map<String,Object> jsonData, List<AlarmStrategy> alarmStrategyList) {
        alarmStrategyList.forEach(alarmStrategy -> {
            String notifyMethod = alarmStrategy.getNotifyMethod();
            //发邮件
            if ("email".equals(notifyMethod)) {
                //要通知的用户
                List<SysUser> notifyUsers = alarmStrategy.getNotifyUsers();
                for (int j = 0; j < notifyUsers.size(); j++) {
                    SysUser sysUser = notifyUsers.get(j);
                    String email = sysUser.getEmail();
                    if (StringUtils.isNotBlank(email)) {
                        sysMailService.sendSimpleMail(email, alarmStrategy.getAlarmMessage(), JSON.toJSONString(jsonData));
                    }
                }
            }
            //发短信
            if ("sms".equals(notifyMethod)) {

            }
        });
    }

}
